<?php

class IndexAction extends CommonAction{

    public function index(){
		$recipe = M('RecipeArticle');
		$recipeinfo = $recipe->limit(5)->where('publish=1 AND recommend=1')->order('downloads desc')->select();
		$this->assign('recipeinfo',$recipeinfo);

		$special = M('SpecialArticle');
		$specialinfo = $special->limit(4)->where('publish=1 AND recommend=1')->order('created desc')->find();
		$this->assign('specialinfo', $specialinfo);
//dump($specialinfo);
		$sauce = M('SauceArticle');
		$sauceinfo = $sauce->limit(4)->where('publish=1 AND recommend=1')->order('created desc')->find();
		$this->assign('sauceinfo', $sauceinfo);
//dump($sauceinfo);
		$kitchen = M('KitchenArticle');
		$kitcheninfo = $kitchen->limit(4)->where('publish=1 AND recommend=1')->order('created desc')->find();
		$this->assign('kitcheninfo', $kitcheninfo);
//dump($kitcheninfo);

		$food = M('FoodArticle');
		$foodmapf['publish'] = 1;
		$foodmapf['onseason'] = 1;
		$foodmapf['sectionid'] = 7;
		$foodinfof = $food->where($foodmapf)->limit(9)->select();
		$this->assign('foodinfof',$foodinfof);

		$foodmapv['publish'] = 1;
		$foodmapv['onseason'] = 1;
		$foodmapv['sectionid'] = 8;
		$foodinfov = $food->where($foodmapv)->limit(9)->select();
		$this->assign('foodinfov',$foodinfov);

		$foodmaps['publish'] = 1;
		$foodmaps['onseason'] = 1;
		$foodmaps['sectionid'] = 9;
		$foodinfos = $food->where($foodmaps)->limit(9)->select();
		$this->assign('foodinfos',$foodinfos);

		$this->display();

	}

	public function login(){
		if(isset($_SESSION['username']) && isset($_SESSION['userid']) && isset($_SESSION['userrole'])){
			$this->redirect('/Index/index');
		}else{
			$this->display();
		}
	}

	function change(){
		if(isset($_POST['action']) && $_POST['action']=='change'){
			if(isset($_POST['email']) && !empty($_POST['email'])){
				$user = M('User');
				$userinfo = $user->getByEmail($_POST['email']);
				if(empty($userinfo)){
					$this->error('不存在该用户');
				}else{
					if(C('ENABLE')==1){

						import("ORG.Net.phpmailer");
						$mail = new PHPMailer(true);
						$mail->IsSMTP();
						$mail->SMTPAuth   = true;
						$mail->Port       = C('EMAILPORT');
						$mail->Host       =C('EMAILSERVER');
						$mail->Username   = C('FROMEMAIL');
						$mail->Password   = C('PASSWORD');
						$mail->AddReplyTo(C('FROMEMAIL'),C('FROMEMAILALIAS'));
						$mail->From       = C('FROMEMAIL');
						$mail->FromName   = C('FROMEMAILALIAS');
						$mail->IsHTML(true);
						$mail->AddAddress($_POST['email']);
						$setmail = M('SetEmail');
						$successmail = $setmail->find(4);
						if($successmail['status']==1){
							$mail->Subject  = $successmail['title'];
							$time = time();
							$body = str_replace('%username%', $userinfo['username'], $successmail['content']);
							$body = str_replace('%changeurl%', '<a target="_blank" href="http://localhost'.__APP__.'/User/change/modify/'.$_POST['email'].'/time/'.md5($time).'">跳转到修改页面</a>', $body);
							$body = str_replace('%date%', date('Y-m-d H:i:s'), $body);
						}
						$mail->MsgHTML($body);
						$mail->Send();
							$data = array('changepass'=>1,'changetime'=>$time);
							$user->where('id='.$userinfo['id'])->setField($data);
					}

					$this->success('修改密码链接已发送,请查收',__APP__.'/Index/login');
				}
			}else{
				$this->error('异常操作');
			}
		}else{
			$this->error('异常操作');
		}
	}

	function forgot(){
		$this->display('password');
	}

	public function joinnow(){
		$this->display('join');
	}

}